﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;

namespace Tunnel_Cross_Section.相关类
{
    public class ReportSepcialHelper : ReportGeneralHelper
    {
        protected override void CopyRange(int max_row_count)
        {
            //donothing 拷贝个屁
        }
        Range startR;
        Range startR1;
        protected override void creat_Excel_graph(System.Data.DataTable ac_dt, System.Data.DataTable cr_dt, int dex, double lc, string cj_pt, string LR_name)
        {
            if (startR == null)
            {
                startR = sheet.Cells[8, 2] as Range;
                startR1 = sheet.Cells[6, 12] as Range;
            }
            for (int i = 0; i < ac_dt.Rows.Count; i++)
            {
                startR.get_Offset(i,0).Value2 = cr_dt.Rows[i][0].ToString();
                startR.get_Offset(i, 1).Value2 = ac_dt.Rows[i][0].ToString();
               
            }
            List<double> Raduis = GetRaduisChange(LR_name, cr_dt);
            for (int i = 0; i < Raduis.Count; i++)
            {
                startR1.get_Offset(i, 0).Value2 = Raduis[i];
            }
            startR = startR.get_Offset(0, 2);
            startR1 = startR1.get_Offset(6, 0);
            if (startR.Column > 9)
            {
                startR = sheet.Cells[startR.Row + 28, 2] as Range;
                startR1 = sheet.Cells[startR.Row - 2, 12] as Range;
            }
        }

        protected override void creat_Excel_graph_L(System.Data.DataTable ac_dt, System.Data.DataTable cr_dt, int dex, double lc, string cj_pt, string LR_name)
        {
            creat_Excel_graph(ac_dt, cr_dt, dex, lc, cj_pt, LR_name);
        }

        private List<double>  GetRaduisChange(string LR_name, System.Data.DataTable cr_dt)
        {
            string qj_prj_no = string.Format("qj_{0}_{1}", do_frm.m_prj.id, do_frm.no);
            string sql;
            sql = string.Format("select A1,A2,A3,A4 from {0} where [断面号]='{1}'", qj_prj_no, LR_name);
            System.Data.DataTable dt = mfrm.m_db.GetDataSet(sql).Tables[0];
            List<double> FAngles = new List<double>();
            double start, end, step;
            do_frm.m_prj.GetAngleStep(out start, out end, out step);
            FAngles.Add(start / 180 * Math.PI);
            FAngles.Add((double)dt.Rows[0]["A1"]);
            FAngles.Add((double)dt.Rows[0]["A2"]);
            FAngles.Add((double)dt.Rows[0]["A3"]);
            FAngles.Add((double)dt.Rows[0]["A4"]);
            FAngles.Add(end / 180 * Math.PI);

            string qj_prj_1 = string.Format("qj_{0}_1", do_frm.m_prj.id);
            sql = string.Format("select R1,R2,R3,R4,R5 from {0} where [断面号]='{1}'", qj_prj_no, LR_name);
            dt = mfrm.m_db.GetDataSet(sql).Tables[0];
            List<double> Raduis = new List<double>();
            Raduis.Add((double)dt.Rows[0]["R1"]);
            Raduis.Add((double)dt.Rows[0]["R2"]);
            Raduis.Add((double)dt.Rows[0]["R3"]);
            Raduis.Add((double)dt.Rows[0]["R4"]);
            Raduis.Add((double)dt.Rows[0]["R5"]);
            int MinRaduisIndex = 0;
            double minraduis = 99;
            int index = 0;
            for (int i = 0; i < FAngles.Count - 1; i++)
            {
                //寻找最小值
                double min = FAngles[i + 1] - FAngles[i];
                if (min < minraduis)
                {
                    minraduis = min;
                    MinRaduisIndex = i;
                }
                int startindex = index;
                index = startindex + (int)Math.Floor(min / (step / 180 * Math.PI)) + 1;
                double change = 0;
                if (do_frm.no > 1)//如果期数大于1,计算半径变化
                {
                    for (int j = startindex; j < index; j++)
                    {
                        try
                        {
                            change += (double)cr_dt.Rows[j][0];
                        }
                        catch
                        {
                            continue;
                        }
                    }
                    change = change / (index - startindex)/1000;
                    Raduis[i] = Raduis[i] + change;
                    Raduis[i] = Math.Round(Raduis[i], 4);
                }
            }
            Raduis.RemoveAt(MinRaduisIndex);
            return Raduis;
        }
    }
}
